import { createStore } from 'vuex'
import createPersistedState from 'vuex-persistedstate'

import cart from './modules/cart'
import user from './modules/user'
import category from './modules/category'
//a模块
const moduleA = {
  state:{
    username:'moduleA'
  },
  getters:{
    newName(state){
      return state.username = 'moduleAA!!!!'
    }
  },
  mutations:{
    updateName(state){
      state.username = 'moduleAAAAAAA'
    }
  }
}
//b模块
const moduleB = {
  namespaced:true,
  state:{
    username:'moduleB'
  },
  getters:{
    newName(state){
      return state.username = 'moduleBBB!!!!'
    }
  },
  mutations: {
    updateName(state){
      state.username = 'ls'
    }
  },
  actions: {
    updateName(ctx){
      setTimeout(()=>{
        ctx.commit('updateName')
      },1000)
    }
  }

}

export default createStore({
  modules: {
    moduleA,
    moduleB,
    cart,
    user,
    category
  },
  //配置插件
  plugins:[
    //默认存储在localStore
    createPersistedState({
      //本地存储名字
      key:'erabbit-client-pc-124-store',
      //指定需要存储的模块
      paths:['user', 'cart']
    })
  ],
  state: {
    username:'xming'
  },
  getters:{
    newName(state){
      return state.username + '123456789'
    }
  },
  mutations: {
    updateName(state){
      state.username = 'ls'
    }
  },
  actions: {
    updateName(ctx){
      setTimeout(()=>{
        ctx.commit('updateName')
      },1000)
    }
  }
})
